package com.juefeng.testWeb.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.juefeng.SMS.pojo.SmsConfig;
import com.juefeng.SMS.pojo.SmsMessage;
import com.juefeng.SMS.pojo.SmsMessageLog;
import com.juefeng.SMS.pojo.SmsRequestParameter;
import com.juefeng.SMS.service.PhoneMessageService;

@Controller
public class TestWebController {
	@Autowired
	private PhoneMessageService messageService;
	
	
	
	@RequestMapping("/send")
	@ResponseBody
	public int sendMessage(@RequestParam(value="phoneNos") String phoneNos ,@RequestParam(value="smsContent") String smsContent,@RequestParam(value="modelType",required=false) String modelType){
		try {
			smsContent =  new String(smsContent.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		SmsRequestParameter smsRequestParameter = new SmsRequestParameter("testWeb", null);
		int sendMessage = messageService.sendMessage(phoneNos, smsContent, 2L,modelType, smsRequestParameter);
		return sendMessage;
	}
	@RequestMapping("/getMessageList")
	@ResponseBody
	public List<SmsMessage> getMessageList(
			@RequestParam(value="sysType",required=false) Long sysType,
			@RequestParam(value="phoneNos",required=false) String phoneNos,
			@RequestParam(value="apiType",required=false) Long apiType,
			@RequestParam(value="modelType",required=false) String modelType,
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows){
		SmsRequestParameter smsRequestParameter = new SmsRequestParameter("testWeb", null);
		return messageService.findMessageList(sysType, phoneNos, null, null, apiType,modelType, page, rows, "ROW_ID", "DESC", 0, smsRequestParameter);
		
	}
	@RequestMapping("/getMessagelogList")
	@ResponseBody
	public List<SmsMessageLog> getMessagelogList(
			@RequestParam(value="sysType",required=false) Long sysType,
			@RequestParam(value="phoneNos",required=false) String phoneNos,
			@RequestParam(value="apiType",required=false) Long apiType,
			@RequestParam(value="modelType",required=false) String modelType,
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows){
		SmsRequestParameter smsRequestParameter = new SmsRequestParameter("testWeb", null);
		return messageService.findMessageLogList(sysType, phoneNos, null, null, apiType, null,modelType,page, rows, null, null, null, smsRequestParameter);
				
		
	}
	
	@RequestMapping("/getMessageConfig/{sysType}/{apiType}")
	@ResponseBody
	public List<SmsConfig> getMessageConfig(
			@PathVariable Long sysType,
			@PathVariable Long apiType,			
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows){
		return messageService.findSMSConfigList(sysType, apiType, page, rows, null, null, null, new SmsRequestParameter("testWeb", null));
	}
	
	
	@RequestMapping("/getMessagelogListCount")
	@ResponseBody
	public Integer getMessagelogListCount(
			@RequestParam(value="sysType",required=false) Long sysType,
			@RequestParam(value="phoneNos",required=false) String phoneNos,
			@RequestParam(value="apiType",required=false) Long apiType,
			@RequestParam(value="modelType",required=false) String modelType,
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows){
		return messageService.findMessageLogListCount(sysType, phoneNos, null, null, apiType, null,modelType, new SmsRequestParameter("testWeb", null));
				
		
	}
	
	@RequestMapping("/updateMessageConfig")
	@ResponseBody
	public Integer updateMessageConfig(){
		SmsConfig smsConfig = new SmsConfig();
		smsConfig.setIsDefault(1);
		smsConfig.setRowId(1);
		return  messageService.updateMessageConfig(smsConfig, new SmsRequestParameter("testWeb", null));
		
	}
}
